
import re

def str2sec(x):
    '''str-time convert to int-second'''
    h,m,s = x.strip().split(':')
    return int(h)*3600 + int(m)*60 + int(s)

file_path = './taxi/_taxi_id.txt'
with open(file_path,'r') as f:
	f_l = f.readlines()
total_num = len(f_l)
print(total_num)

all_taxi = {}

for line in f_l:
    print(line)
    taxi_list = line.strip().split(';')
    taxi_id = taxi_list[0]
    # if not has this-id, make a new
    if isinstance(all_taxi.get(taxi_id,None),type(None)):
        all_taxi.update({taxi_id:[]})
    print(taxi_list)
    tmp_time = (taxi_list[1].split()[0],re.split('[+.]',taxi_list[1].split()[1])[0]) # ('y-m-d','h:m:s')
    taxi_time_date = tmp_time[0]
    taxi_time_hour = tmp_time[1]
    taxi_time_second = str2sec(tmp_time[1])
    
    taxi_loc = taxi_list[-1].replace('POINT(','').replace(')','').split()
    taxi_locx = eval(taxi_loc[0]) 
    taxi_locy = eval(taxi_loc[1]) 
    all_taxi[taxi_id].append((taxi_locx,taxi_locy,taxi_time_date,taxi_time_hour,taxi_time_second))
    
    
taxi_num = len(all_taxi)
detail_path = './taxi/'

new_sep_date = {}

for k,v in all_taxi.items():
    tmp_k = k
    tmptmp = 1
    for idx,line in enumerate(v[:-1]):
        if isinstance(new_sep_date.get(tmp_k,None),type(None)):
            new_sep_date.update({tmp_k:[]})
        new_sep_date[tmp_k].append(list(line))  
        if line[2] != v[idx+1][2]:
            tmp_k = tmp_k+'00'+str(tmptmp)
            tmptmp += 1

with open(detail_path+'_taxi_id.txt','w+',newline='') as tmp:
    for k,v in new_sep_date.items():
        with open(detail_path+str(k)+'.txt','w+',newline='') as temp:
            for line in v:
                s = str(line[0])+' '+str(line[1])+' '+str(line[2])+' '+str(line[3])+' '+str(line[4])
                temp.write(s)
                temp.write('\r\n')
        tmp.write(k)
        tmp.write('\r\n')





